﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KerriganDataContract;
using KerriganDataContract.Forum;

namespace KerriganForum
{
    public partial class EditMessage : System.Web.UI.Page
    {
        private string ACTIVITY
        {
            get
            {
                if (ViewState["Activity"] == null)
                    ViewState["Activity"] = null;
                return (string)ViewState["Activity"];
            }
            set
            {
                ViewState["Activity"] = value;
            }
        }
        private int ID
        {
            get
            {
                if (ViewState["ID"] == null)
                    ViewState["ID"] = -1;
                return (int)ViewState["ID"];
            }
            set
            {
                ViewState["ID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ID = Convert.ToInt32(Request.QueryString["ID"]);
                ACTIVITY = Request.QueryString["Activity"];

                using (ServiceProxy Srv = new ServiceProxy())
                {
                    ForumMessageDC res = Srv.Service.GetMessageById(ID);
                    if (ACTIVITY == "edit")
                    {
                        txtNameMessage.InnerHtml = res.Message;

                    }
                }
            }

        }

        protected void btnOkay_Click(object sender, EventArgs e)
        {
            try
            {
                if (ACTIVITY == "edit")
                {
                    MessageEdit();
                    ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);
                }

            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "onload", "onError();", true);
            }
        }



        private void MessageEdit()
        {
            using (ServiceProxy Srv = new ServiceProxy())
            {
                ForumDC bo = new ForumDC();
                bo.Message = new ForumMessageDC();
                bo.Message.Message = txtNameMessage.InnerText;
                bo.Message.Date = DateTime.Now;
                Srv.Service.UpdateMessage(ID, bo);


            }

        }
    }
}